<?php session_start();
# Logging in with Google accounts requires setting special identity, so this example shows how to do it.
require 'require/openid.php';

try {
    # Change 'localhost' to your domain name.
    $openid = new LightOpenID('localhost');  //demos.w3lessons.com
	
		$openid->required = array(
		'namePerson',
		'namePerson/first',
		'namePerson/last',
		'contact/email',
		);

    if(!$openid->mode) {
		
	if(@$_GET['auth']=="google")
    {
		$_SESSION['auth']="Google";
        $openid->identity = 'https://www.google.com/accounts/o8/id';
        header('Location: ' . $openid->authUrl());
	}elseif(@$_GET['auth']=="yahoo")
	{
		$_SESSION['auth']="Yahoo";
		$openid->identity ='yahoo.com';
		header("Location:".$openid->authUrl());
	}

    } elseif($openid->mode == 'cancel') {
        echo 'User has canceled authentication!';
    } else {
		 $external_login=$openid->getAttributes();
		 $_SESSION['name']=$external_login['namePerson/first']." ".$external_login['namePerson/last'];
		 $_SESSION['email']=$external_login['contact/email'];
		 header("Location:account-home.php");
		 exit();
		 
    }
} catch(ErrorException $e) {
    echo $e->getMessage();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>OpenID Login with with Google Account or Yahoo ID</title>
<style type="text/css">
<!--
body,td,th {
	font-family: Verdana, Geneva, sans-serif;
	color: #333;
	font-size: 11px;
}

input[type='text'],input[type='password']{ border:1px solid #CCC;}
.center {
	text-align: center;
}

-->
</style></head>
<body>
<form action="index.php" method="post" >

<h1 class="center">My Website</h1>
<h2 class="center">Website Account Login with Google, Yahoo Demo</h2>
<table width="200" border="0" align="center" cellpadding="5" cellspacing="0" style="border:1px solid #CCC;">
  <tr>
    <td bgcolor="#D6D6D6"><strong>Account Login</strong></td>
  </tr>
  <tr>
    <td>

      <!--table width="100%" border="0" cellspacing="0" cellpadding="5">
        <tr>
          <td>E-mail address</td>
          <td><label>
            <input type="text" name="username" id="username" />
          </label></td>
        </tr>
        <tr>
          <td>Password</td>
          <td><label>
            <input type="password" name="password" id="password" />
          </label></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><label>
            <input type="submit" name="button" id="button" value="Submit" />
            </label></td>
        </tr>
        </table-->

<?php
if(isset($_POST['ok']))
{
 if($_POST['username'] == NULL)
 {
  echo "Please enter your username<br />";
 }
 else
 {
  $u=$_POST['username'];
 }
 if($_POST['password'] == NULL)
 {
  echo "Please enter your password<br />";
 }
 else
 {
  $p=$_POST['password'];
 }
 if($u && $p)
 {
  $conn=mysql_connect("localhost","root","root") or die("can't connect this database");
  mysql_select_db("thoitrang",$conn);
  $sql="select * from account where username='".$u."' and password='".$p."'";
  $query=mysql_query($sql);
  if(mysql_num_rows($query) == 0)
  {
   echo "Username or password is not correct, please try again";
  }
  else
  {
   $row=mysql_fetch_array($query);
   session_start();
   session_register("userid");
   session_register("level");
   $_SESSION['userid'] = $row[id];
   $_SESSION['level'] = $row[level];
   $_SESSION['username'] = $u;
	if($row[level]== 2)
 		header("location:account-home.php");
	else
		header("location:update_user.php");
  }
 }
}
?>


	<!--form action=index.php method=post>
	Username: <input type=text name=username size=25 /><br />
	Password: <input type=password name=password size=25 /><br />
	<input type=submit name=ok value="Dang Nhap" />
	</form> -->

	<table width="100%" border="0" cellspacing="0" cellpadding="5">
        <tr>
          <td>User Name</td>

          <td><label>
            <input type="text" name="username" id="username" />
          </label></td>
        </tr>
        <tr>

          <td>Password</td>
          <td><label>
            <input type="password" name="password" id="password" />
          </label></td>

        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><label>
            <input type="submit" name="ok" id="button" value="Submit" />

            </label></td>
        </tr>
        </table>


	</td>
  </tr>
  <tr>
    <td bgcolor="#F6F6F6"><img src="open_login.jpg" alt="OAuth Login" width="280" height="85" border="0" usemap="#Map" /></td>
  </tr>
</table>


<p>
  <map name="Map" id="Map">
    <area shape="rect" coords="13,45,162,75" href="index.php?auth=google" alt="Login with Google Account" />
    <area shape="rect" coords="171,46,269,74" href="index.php?auth=yahoo" alt="Login with Yahoo ID" />
  </map>
</p>

</form>

</body>
</html>
